package com.xy.demo.transaction.generator.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.xy.demo.transaction.generator.domain.T1;
import com.xy.demo.transaction.generator.domain.T2;
import com.xy.demo.transaction.generator.service.T1Service;
import com.xy.demo.transaction.generator.service.T2Service;
import com.xy.demo.transaction.generator.service.TService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

@Service
public class TServiceImpl implements TService {

    @Autowired
    private T1Service t1Service;
    @Autowired
    private T2Service t2Service;

    @SneakyThrows
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void insert(boolean setExp) {
        String id = IdWorker.get32UUID();
        insertT1(id);
        setExpMethod(setExp);
        insertT2(id);
    }

    public void insertT1(String id) {
        T1 t1 = new T1();
        t1.setId(id);
        t1.setName("小明");
        t1Service.customInsert(t1);
        setExpMethod(true);
    }
    private void insertT2(String id) {
        T2 t2 = new T2();
        t2.setId(id);
        t2.setGender("男");
        t2Service.customInsert(t2);
    }

    @SneakyThrows
    public void setExpMethod(boolean setExp) {
        if (setExp) {
            throw new Exception("自定义异常");
        }
    }
}
